package com.epalmpay.service.posapi;

import com.epalmpay.entity.BonusSendRule;
import com.epalmpay.entity.BonusVerifyRule;
import com.epalmpay.mapper.BonusSendRuleMapper;
import com.epalmpay.mapper.BonusVerifyRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by lifayi on 2017/9/28.
 */
@Service
public class PosApiServiceCacheable {
    @Autowired
    private BonusVerifyRuleMapper bonusVerifyRuleMapper;
    @Autowired
    private BonusSendRuleMapper bonusSendRuleMapper;

    BonusVerifyRule shopBonusVerifyRule(Long sid, Long groupId) {

        //查询门店是否自己设置规则
        BonusVerifyRule bonusVerifyRule=null;
        bonusVerifyRule=bonusVerifyRuleMapper.qyeryShopVerifyRule(sid);
        if(bonusVerifyRule!=null){
                return bonusVerifyRule;
        }else{

            BonusVerifyRule bonusVerifyRuleDefault = bonusVerifyRuleMapper.getDefaultByGroupId(groupId);
            bonusVerifyRule = bonusVerifyRuleMapper.getRuleByShopId(sid, groupId);
            if(bonusVerifyRule != null) {
                bonusVerifyRule.setVerifyRate(bonusVerifyRuleDefault.getVerifyRate());
                return bonusVerifyRule;
            } else {
                return bonusVerifyRuleDefault;
            }
        }
    }

    BonusSendRule shopBonusSendRule(Long sid, Long groupId) {
        BonusSendRule bonusSendRule = bonusSendRuleMapper.getRuleByShopId(sid, groupId);
        if(bonusSendRule != null) {
            return bonusSendRule;
        } else {
            return bonusSendRuleMapper.getDefaultByGroupId(groupId);
        }
    }
}
